package com.superjson.showpro.base;

import com.superjson.showpro.shared.result.Result;
import com.superjson.showpro.shared.result.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created By zby on 11:42 2019/6/21
 * 这是基本服务层的实现类
 *
 * @author baoya
 * @since 1.1.0
 */
@SuppressWarnings("All")
public class GenericServiceImpl<T> implements GenericService<T> {
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 这是基本dao层的接口
     */
    protected GenericDao<T> dao;

    public GenericServiceImpl() {
    }

    /**
     * Created By zby on 11:43 2019/6/21
     *
     * @param genericDao dao层的基本接口
     */
    public GenericServiceImpl(GenericDao<T> genericDao) {
        this.dao = genericDao;
    }


    /**
     * Created By zby on 11:43 2019/6/21
     *
     * @param id 当前类对象的主键id
     * @return 获取当前实现类的对象
     */
    @Override
    public Result<T> get(final Long id) {

        final T t = dao.get(id);

        return ResultUtil.buildResult(t);
    }

    /**
     * Created By zby on 11:45 2019/6/21
     * 根据以逗号分割的主键获取当前类对象的集合，比如1,2,3...
     *
     * @param ids 类对象的以逗号分割的主键，1,2,3
     */
    @Override
    public Result<List<T>> get(final String ids) {
        final List<T> t = dao.get(ids);
        return ResultUtil.buildResult(t);
    }


    /**
     * Created By zby on 12:13 2019/6/21
     * 判断当前对象是否存在
     *
     * @param id 具体实现类对象的id
     * @return 如果当前对象存在，返回true，不存在，返回false
     */
    @Override
    public Result<Boolean> exists(final Long id) {

        boolean isExist = dao.exists(id);

        return ResultUtil.buildSuccess(isExist);
    }

    /**
     * Created By zby on 12:14 2019/6/21
     * 保存当前对象
     *
     * @param object 具体实现类对象
     */
    @Override
    public Result<T> save(T object) {

        final T t = dao.save(object);

        return ResultUtil.buildSuccess(t);
    }

    /**
     * Created By zby on 12:15 2019/6/21
     * 更新当前对象
     *
     * @param object 实现类的对象
     */
    @Override
    public Result<T> update(T object) {

        dao.update(object);

        return ResultUtil.buildSuccess(object);
    }

    /**
     * Created By zby on 12:15 2019/6/21
     * 删除指定主键id的对象
     *
     * @param id                 当前对象id
     * @param isPhysicallyDelete false 表示逻辑删除，即将is_deleted字段置为1，
     *                           true 物理删除 将编号为id的数据对象删除
     */
    @Override
    public Result delete(final Long id, final boolean isPhysicallyDelete) {

        if (dao.delete(id, isPhysicallyDelete) == 1) {
            return ResultUtil.buildSuccess();
        } else {
            return ResultUtil.buildGenericFailed();
        }
    }

    /**
     * Created By zby on 12:15 2019/6/21
     * 删除指定以逗号分割的主键字符串的对象
     *
     * @param ids                当前一系列对象ids
     * @param isPhysicallyDelete false 表示逻辑删除，即将is_deleted字段置为1，
     *                           true 物理删除 将编号为id的数据对象删除
     */
    @Override
    public Result deleteBatch(String ids, boolean isPhysicallyDelete) {
        if (StringUtils.isBlank(ids)) {
            return ResultUtil.buildGenericFailed();
        }
        String[] idList = ids.split("\\,");
        for (String id : idList) {
            if (!StringUtils.isNumeric(id)) {
                continue;
            }
            delete(Long.parseLong(id), isPhysicallyDelete);
        }
        return ResultUtil.buildSuccess();
    }

}
